package com.property.system.controller;

import com.property.common.annotation.Log;
import com.property.common.config.PropertyConfig;
import com.property.common.core.controller.BaseController;
import com.property.common.core.domain.AjaxResult;
import com.property.common.core.page.TableDataInfo;
import com.property.common.enums.BusinessType;
import com.property.common.utils.file.FileUploadUtils;
import com.property.common.utils.file.FileUtils;
import com.property.system.domain.SysCarScan;
import com.property.system.service.ISysCarScanService;
import com.property.system.utils.OcrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 车辆扫描信息 控制层
 */
@RestController
@RequestMapping("/system/carScan")
public class SysCarScanController extends BaseController {

    @Autowired
    private ISysCarScanService carScanService;

    /**
     * 获取车牌扫描记录列表
     */
    //@PreAuthorize("@ss.hasPermi('system:carScan:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCarScan carScan) {
        startPage();
        List<SysCarScan> list = carScanService.selectCarScanList(carScan);
        return getDataTable(list);
    }

    @GetMapping("/listByUser")
    public TableDataInfo listByUser(SysCarScan carScan) {
        carScan.setUserId(getUserId());
        startPage();
        List<SysCarScan> list = carScanService.selectCarScanList(carScan);
        return getDataTable(list);
    }


    /**
     * 获取车牌扫描记录详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:carScan:query')")
    @GetMapping("/{scanId}")
    public AjaxResult getInfo(@PathVariable Long scanId) {
        return success(carScanService.selectCarScanById(scanId));
    }

    /**
     * 根据用户ID获取车牌扫描记录
     */
    //@PreAuthorize("@ss.hasPermi('system:carScan:query')")
    @GetMapping("/byUser")
    public AjaxResult getByUser() {
        return success(carScanService.selectCarScanByUserId(getUserId()));
    }

    /**
     * 车辆入场接口
     */
    //@PreAuthorize("@ss.hasPermi('system:carScan:add')")
    @Log(title = "车辆扫描信息", businessType = BusinessType.INSERT)
    @PostMapping("/addEntryCar")
    public AjaxResult add(@Validated @RequestBody SysCarScan carScan) {
        carScan.setUserId(getUserId());
        return toAjax(carScanService.addEntryCar(carScan));
    }
    /**
     * 车辆出场接口
     */
    @Log(title = "车辆扫描信息", businessType = BusinessType.INSERT)
    @PostMapping("/exitCar")
    public AjaxResult exitCar(@Validated @RequestBody SysCarScan carScan) {
        carScan.setUserId(getUserId());
        return toAjax(carScanService.exitCar(carScan));
    }
    /**
     * 车牌识别接口
     */
    @PostMapping("/carScan")
    public AjaxResult carScan(MultipartFile file) {
        if (file !=null && !file.isEmpty()) {
            try {
                //上传图片路径
                String filePath = PropertyConfig.getUploadPlatePath();
                String fileName = FileUploadUtils.upload(filePath, file);
                String newFileName = FileUtils.getName(fileName);
                Map<String, Object> plate = OcrUtil.plateLicense(PropertyConfig.getProfile() + fileName.replace("/profile", ""));
                if (plate.isEmpty()) {
                    return error("车牌识别失败，请联系管理员！");
                }
                SysCarScan sysCarScan = new SysCarScan();
                sysCarScan.setPicture(fileName);
                sysCarScan.setPlateNum(plate.get("number").toString());
                sysCarScan.setPlateColor(plate.get("color").toString());
                return AjaxResult.success(sysCarScan);
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            }

        }
        return error("车牌识别失败，请联系管理员！");
    }


    /**
     * 修改车牌扫描记录
     */
    //@PreAuthorize("@ss.hasPermi('system:carScan:edit')")
    @Log(title = "车辆扫描信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysCarScan carScan) {
        carScan.setUserId(getUserId());
        return toAjax(carScanService.updateCarScan(carScan));
    }

    /**
     * 删除车牌扫描记录
     */
    //@PreAuthorize("@ss.hasPermi('system:carScan:remove')")
    @Log(title = "车辆扫描信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scanIds}")
    public AjaxResult remove(@PathVariable Long[] scanIds) {
        return toAjax(carScanService.deleteCarScanByIds(scanIds));
    }
}
